import {PAGE_TEN} from '../constants';
import {PAGE_CODE} from './constants';
import {
    getCurrentUserPagePermissionsBegin,
    getCurrentUserPagePermissionsSuccess,
    getCurrentUserPagePermissionsFail,
    getCurrentUserPagePermissionsApi,
} from 'Shared/actions/currentUserContext';
import * as api from './api';

export const SAVE_QUERY_CONDITION = 'SAVE_QUERY_CONDITION';
export const saveQueryCondition = obj => ({
    type: SAVE_QUERY_CONDITION,
    obj
});

export const RESET_QUERY_PANEL = 'RESET_QUERY_PANEL';
export const resetQueryPanel = () => ({
    type: RESET_QUERY_PANEL
});

export const GET_INIT_BEGIN = 'GET_INIT_BEGIN';
export const GET_INIT_SUCCESS = 'GET_INIT_SUCCESS';
export const GET_INIT_FAIL = 'GET_INIT_FAIL';
export const getInitDataBegin = () => ({
    type: GET_INIT_BEGIN
});
export const getInitDataSuccess = (data, conditions) => ({
    type: GET_INIT_SUCCESS,
    data,
    conditions
});
export const getInitDataFail = message => ({
    type: GET_INIT_FAIL,
    message
});
export const getInitData = () => dispatch => {
    dispatch(getInitDataBegin());
    const init = api.getInitData().then(res => {
        if(res.ok)
            dispatch(getInitDataSuccess(res.data));
        return res.ok;
    });
    dispatch(getCurrentUserPagePermissionsBegin(PAGE_CODE));
    const permissions = getCurrentUserPagePermissionsApi(PAGE_CODE).then(res => {
        if(res.isOk)
            dispatch(getCurrentUserPagePermissionsSuccess(res.data));
        else
            dispatch(getCurrentUserPagePermissionsFail(res.statusCode, res.message));
    });
    return Promise.all([init, permissions]).then(values => values[0] && values[1]);
};

export const GET_LIST_DATA_BEGIN = 'GET_LIST_DATA_BEGIN';
export const GET_LIST_DATA_SUCCESS = 'GET_LIST_DATA_SUCCESS';
export const GET_LIST_DATA_FAIL = 'GET_LIST_DATA_FAIL';

const getListDataBegin = () => ({
    type: GET_LIST_DATA_BEGIN
});

const getListDataSuccess = (condition, data) => ({
    type: GET_LIST_DATA_SUCCESS,
    condition,
    data
});

const getListDataFail = () => ({
    type: GET_LIST_DATA_FAIL,
});

const getListData = condition => {
    if(!condition)
        return;

    let isDesc = undefined;
    if(condition.sortOrder && condition.sortBy)
        isDesc = condition.sortOrder === 'descend';

    const queryCondition = {
        code: condition.code,
        name: condition.name,
        isEOL: condition.isEOL,
        categoryCode: condition.categoryCode,
        status: condition.status,
        warrantyPartType: condition.warrantyPartType,
        isDangerousGoods: condition.isDangerousGoods,
        channel: condition.channel,
        pageIndex: condition.pageIndex,
        pageSize: condition.pageSize,
        sortField: condition.sortBy,
        isDesc
    };

    return api.getParts(queryCondition).then(res => ({
        ok: res.ok,
        data: res.data
    }));
};

export const searchList = orderCon => (dispatch, getState) => {
    let condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    const pageTableCondition = getState().getIn(['page', 'appState', 'pageTableCondition']).toJS();
    condition.pageSize = pageTableCondition.pageSize || PAGE_TEN.size;
    condition.sortBy = pageTableCondition.sortBy || condition.sortBy;
    condition.sortOrder = pageTableCondition.sortOrder || condition.sortOrder;
    
    if(orderCon)
        condition = Object.assign({}, condition, orderCon);
    dispatch(getListDataBegin());

    return getListData(condition).then(result => {
        if(result && result.ok)
            dispatch(getListDataSuccess(condition, result.data));
        else
            dispatch(getListDataFail());
    });
};

export const changePageForList = con => (dispatch, getState) => {
    dispatch(getListDataBegin());
    const state = getState();
    let condition = state.getIn(['page', 'appState', 'pageTableCondition']).toJS();

    if(con)
        condition = Object.assign({}, condition, con);
    return getListData(condition).then(result => {
        if(result && result.ok)
            dispatch(getListDataSuccess(condition, result.data));
        else
            dispatch(getListDataFail());
    });
};

export const GET_DETAIL_FAIL = 'GET_DETAIL_FAIL';
const getDetailFail = () => ({
    type: GET_DETAIL_FAIL,
});

export const getDetailForDetailPage = id => dispatch => api.getPartsDetail(id).then(res => {
    if(res.ok) {
        if(typeof res.data.chemicalCheck === 'boolean')
            res.data.chemicalCheck = (res.data.chemicalCheck === true ? 1 : 0);
        if(typeof res.data.airMagneticCheck === 'boolean')
            res.data.airMagneticCheck = (res.data.airMagneticCheck === true ? 1 : 0);
        if(typeof res.data.businessCheck === 'boolean')
            res.data.businessCheck = (res.data.businessCheck === true ? 1 : 0);
        if(typeof res.data.dangerousGoods === 'boolean')
            res.data.dangerousGoods = (res.data.dangerousGoods === true ? 1 : 0);
        if(typeof res.data.remoteControl === 'boolean')
            res.data.remoteControl = (res.data.remoteControl === true ? 1 : 0);
        if(typeof res.data.cvJointKit === 'boolean')
            res.data.cvJointKit = (res.data.cvJointKit === true ? 1 : 0);
        return res.data;
    }
    dispatch(getDetailFail());
    return null;
});

export const exportBasicData = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    if(!condition)
        return;
    const queryCondition = {
        code: condition.code,
        name: condition.name,
        status: condition.status,
        isEOL: condition.isEOL,
        categoryCode: condition.categoryCode,
        warrantyPartType: condition.warrantyPartType,
        isDangerousGoods: condition.isDangerousGoods,
        channel: condition.channel,
    };
    return api.exportBasicData(queryCondition).then(res => {
        if(res.ok)
            return res.data;
    });
};

export const exportCheckedPopertiesData = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    if(!condition)
        return;
    const queryCondition = {
        code: condition.code,
        name: condition.name,
        status: condition.status,
        isEOL: condition.isEOL,
        categoryCode: condition.categoryCode,
        warrantyPartType: condition.warrantyPartType,
        isDangerousGoods: condition.isDangerousGoods,
        channel: condition.channel,
    };
    return api.exportCheckedPopertiesData(queryCondition).then(res => {
        if(res.ok)
            return res.data;
    });
};

export const exportSalesAttributesPopertiesData = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    if(!condition)
        return;
    const queryCondition = {
        code: condition.code,
        name: condition.name,
        status: condition.status,
        isEOL: condition.isEOL,
        categoryCode: condition.categoryCode,
        warrantyPartType: condition.warrantyPartType,
        isDangerousGoods: condition.isDangerousGoods,
        channel: condition.channel,
    };
    return api.exportSalesAttributesPopertiesData(queryCondition).then(res => {
        if(res.ok)
            return res.data;
    });
};

export const exportPartSalesPriceData = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    if(!condition)
        return;
    const queryCondition = {
        code: condition.code,
        name: condition.name,
        status: condition.status,
        isEOL: condition.isEOL,
        categoryCode: condition.categoryCode,
        warrantyPartType: condition.warrantyPartType,
        isDangerousGoods: condition.isDangerousGoods,
        channel: condition.channel,
    };
    return api.exportPartSalesPriceData(queryCondition).then(res => {
        if(res.ok)
            return res.data;
    });
};

export const START_BATCH_RECEIVE = 'START_BATCH_RECEIVE';
export const startBatchFreeze = () => ({
    type: START_BATCH_RECEIVE
});

export const START_BATCH_THAW = 'START_BATCH_THAW';
export const startBatchThaw = () => ({
    type: START_BATCH_THAW
});

export const CLEAR_BATCH_DATA = 'CLEAR_BATCH_DATA';
export const clearBatchData = () => ({
    type: CLEAR_BATCH_DATA,
});
export const CLEAR_BATCH_THAW = 'CLEAR_BATCH_THAW';
export const clearBatchThawData = () => ({
    type: CLEAR_BATCH_THAW,
});

/*==============================导入模板action================================= */
export const salesAttributesImportTemp = () => () => api.salesAttributesImportTemp();
export const checkAttributesImportTemp = () => () => api.checkAttributesImportTemp();
export const produceAttributesImportTemp = () => () => api.produceAttributesImportTemp();

